package com.sh.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import sh.dto.MyParam;
import sh.dto.Response;
import sh.dto.ResultDto;
import sh.entity.Classroom;
import com.sh.admin.dao.ClassroomDao;
import com.sh.admin.service.ClassroomService;
import org.springframework.stereotype.Service;
import com.sh.admin.config.MyValidator;

import javax.annotation.Resource;
import java.util.List;

/**
 * 教室表(Classroom)表服务实现类
 *
 * @author makejava
 * @since 2021-05-10 14:49:57
 */
@Service("classroomService")
public class ClassroomServiceImpl implements ClassroomService {
    @Resource
    private ClassroomDao classroomDao;

    @Resource
    private MyValidator myValidator;

    @Resource
    private MyParam myParam;

    /**
     * 通过ID查询单条数据
     *
     * @param classroomId 主键
     * @return 实例对象
     */
    @Override
    public Classroom queryById(Integer classroomId) {
        return this.classroomDao.queryById(classroomId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Classroom> queryAllByLimit(int offset, int limit) {
        return this.classroomDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param classroom 实例对象
     * @return 实例对象
     */
    @Override
    public ResultDto insert(Classroom classroom) {
        int i = classroomDao.insert(classroom);
        if (i <= 0 ){
            return Response.error(1002,"添加失败");
        }
        return Response.success(200,"添加成功");
    }

    /**
     * 修改数据
     *
     * @param classroom 实例对象
     * @return 实例对象
     */
    @Override
    public ResultDto update(Classroom classroom) {
        int i = classroomDao.insert(classroom);
        if (i <= 0 ){
            return Response.error(1002,"修改失败");
        }
        return Response.success(200,"修改成功");
    }

    /**
     * 通过主键删除数据
     *
     * @param classroom 主键
     * @return 是否成功
     */
    @Override
    public ResultDto deleteById(Classroom classroom) {
        int i = classroomDao.deleteById(classroom.getClassroomId());
        if (i <= 0 ){
            return Response.error(1002,"删除失败");
        }
        return Response.success(200,"删除成功");
    }

    /**
     * 查找所有教室
     * @param classroom
     * @return
     */
    @Override
    public ResultDto findAll(Classroom classroom) {
        myValidator.validate(classroom);
        MyParam myParam = new MyParam();
        PageHelper.startPage(myParam.getPage(),myParam.getLimit());
        List<Classroom> classrooms = classroomDao.findAll(classroom);
        PageInfo<Classroom> classroomPageInfo = new PageInfo<>(classrooms);
        if (classrooms.isEmpty()){
            return Response.error(1001,"未找到相关数据");
        }
        return Response.success(200,"找到相关的数据",classroomPageInfo.getList(),Integer.valueOf(classroomPageInfo.getTotal()+""));
    }

    /**
     * 查找未使用的数据
     * @return
     */
    @Override
    public ResultDto findNotUse() {
        List<Classroom> classrooms = classroomDao.findNotUse();
        if (classrooms.isEmpty()){
            return Response.error(1001,"未找到相关数据");
        }
        return Response.success(200,"找到相关的数据",classrooms);
    }
}
